<template>
  <div>
    <Navbar :title="title" left-text="返回" left-arrow @click-left="goBack"></Navbar>

    <van-address-edit :area-list="areaList" show-set-default show-delete :address-info="addressInfo"
      :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" @delete="onDelete" />
  </div>
</template>

<script>
import Navbar from "../Navbar.vue";
import { areaList } from "@vant/area-data";

export default {
  name: "AddressEdit",
  data() {
    return {
      title: "修改收货地址",
      areaList,
      addressid: this.$route.query.id ? this.$route.query.id : 0,
      addressInfo: {},
      user: this.$cookies.get("ShopUser"),
      idArr: this.$route.query.idArr ? this.$route.query.idArr : 0,
      action: this.$route.query.action ? this.$route.query.action : 'user',
      nums: this.$route.query.nums ? this.$route.query.nums : 0
    };
  },
  methods: {
    async onSave(val) {
      let data = {
        id: this.addressid,
        consignee: val.name,
        mobile: val.tel,
        code: val.areaCode,
        address: val.addressDetail,
        status: val.isDefault ? 1 : 0,
        userid: this.user.id,
      };

      let res = await this.$api.edit(data);

      if (res.code === 1) {
        this.$notify({
          type: "success",
          message: res.msg,
          duration: 1000,
          onClose: () => {
            // this.$router.push(res.url);
            if (this.action === 'user') {
              this.$router.push({ name: 'AddressBase', query: { action: this.action } });
            } else if (this.idArr != null) {
              this.$router.push({ name: 'AddressBase', query: { action: this.action, nums: this.nums, idArr: this.idArr } });
            }
          },
        });
        return false;
      } else {
        this.$notify({
          type: "danger",
          message: res.msg,
          duration: 1000,
        });
        return false;
      }
    },
    onDelete() {
      // console.log(this.addressid);
      this.$dialog
        .confirm({
          title: "确认删除？",
          message: "您确认删除当前收货地址？",
        })
        .then(() => {
          this.$toast.loading({
            message: "删除中...",
            forbidClick: true,
            onClose: async () => {
              let res = await this.$api.delete({ id: this.addressid });

              if (res.code === 1) {
                this.$notify({
                  type: "success",
                  message: res.msg,
                  duration: 1000,
                  onClose: () => {
                    if (this.action === 'user') {
                      this.$router.push({ name: 'AddressBase', query: { action: this.action } });
                    } else if (this.idArr != null) {
                      this.$router.push({ name: 'AddressBase', query: { action: this.action, nums: this.nums, idArr: this.idArr } });
                    }
                  },
                });
                return false;
              } else {
                this.$notify({
                  type: "danger",
                  message: res.msg
                });
                return false;
              }
            }
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    goBack() {
      if (this.action === 'user') {
        this.$router.push({ name: 'AddressBase', query: { action: this.action } });
      } else if (this.idArr != null) {
        this.$router.push({ name: 'AddressBase', query: { action: this.action, nums: this.nums, idArr: this.idArr } });
      } else {
        console.log(-1);
        this.$router.go(-1)
      }
    },
    async AddressInfo(id) {
      let res = await this.$api.AddressInfo({ id });

      if (res.code === 1) {
        this.addressInfo = {
          name: res.data.consignee,
          tel: res.data.mobile,
          addressDetail: res.data.address,
          isDefault: res.data.status == 1 ? true : false,
        };

        if (res.data.province) {
          this.addressInfo.province = res.data.province.name;
          this.addressInfo.areaCode = res.data.province.toString();
        }

        if (res.data.city) {
          this.addressInfo.city = res.data.citys.name;
          this.addressInfo.areaCode = res.data.city.toString();
        }

        if (res.data.district) {
          this.addressInfo.county = res.data.districts.name;
          this.addressInfo.areaCode = res.data.district.toString();
        }

      } else {
        this.$notify({
          type: "danger",
          message: res.msg,
          duration: 1500,
          onClose: () => {
            this.$router.push(res.url);
          }
        });
        return false;
      }
    },
  },
  created() {
    this.AddressInfo(this.addressid);
  },
  computed: {
    address() { },
  },
  components: {
    Navbar,
  },
};
</script>

<style scoped>
</style>
